package com.ctd.springframework.core.io.support;

import com.ctd.springframework.core.io.DefaultResourceLoader;
import com.ctd.springframework.core.io.Resource;
import com.ctd.springframework.core.io.ResourceLoader;
import com.ctd.springframework.util.Assert;
import com.ctd.springframework.util.ClassUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Method;
import java.net.URL;

/**
 * todo 虚位以待，请打造属于自己的品牌
 * PathMatchingResourcePatternResolver
 *
 * @author chentudong
 * @date 2019/3/26 0:15
 */
public class PathMatchingResourcePatternResolver implements ResourcePatternResolver {
    private static final Log logger = LogFactory.getLog(PathMatchingResourcePatternResolver.class);

    private static Method equinoxResolveMethod;
    private final ResourceLoader resourceLoader;

    static {
        try {
            // Detect Equinox OSGi (e.g. on WebSphere 6.1)
            Class<?> fileLocatorClass = ClassUtils.forName("org.eclipse.core.runtime.FileLocator",
                    PathMatchingResourcePatternResolver.class.getClassLoader());
            equinoxResolveMethod = fileLocatorClass.getMethod("resolve", URL.class);
            logger.trace("Found Equinox FileLocator for OSGi bundle URL resolution");
        } catch (Throwable ex) {
            equinoxResolveMethod = null;
        }
    }

    /**
     * Create a new PathMatchingResourcePatternResolver.
     * <p>ClassLoader access will happen via the thread context class loader.
     *
     * @param resourceLoader the ResourceLoader to load root directories and
     *                       actual resources with
     */
    public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
        Assert.notNull(resourceLoader, "ResourceLoader must not be null");
        this.resourceLoader = resourceLoader;
    }

    public PathMatchingResourcePatternResolver() {
        this.resourceLoader = new DefaultResourceLoader();
    }

    public PathMatchingResourcePatternResolver(ClassLoader classLoader) {
        this.resourceLoader = new DefaultResourceLoader(classLoader);
    }

    /**
     * Return the ResourceLoader that this pattern resolver works with.
     */
    public ResourceLoader getResourceLoader() {
        return this.resourceLoader;
    }

    /**
     * getResource
     *
     * @param location location
     * @return Resource
     */
    @Override
    public Resource getResource(String location) {
        return null;
    }

    /**
     * 获取ClassLoader
     *
     * @return ClassLoader
     */
    @Override
    public ClassLoader getClassLoader() {
        return getResourceLoader().getClassLoader();
    }
}
